<?php declare(strict_types=1);

namespace App\Api\V2\Controller\Bill\User;


use App\Api\Controllers\BaseController;
use App\Services\Count\UserBillCountService;
use App\Services\Count\UserService;
use App\Services\PrintSqlService;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;

class TrendChartController extends BaseController
{
    use ResponseTrait;

    //父店变化趋势
    public function pstoreTrend(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $agent_id = $user->user_id;  //查找下级所有的数据
            if (!empty($request->get("agent_id"))) {
                $agent_id = $request->get("agent_id");
            }
            $userService = new UserService();
            list($idValues, $column) = $userService->handleUserAccountRelation((int)$agent_id);
            unset($column);
            list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
            $where = [
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
                "store_pid" => $request->get("store_pid", ""),
                "curHdIndex" => 0,
                "type" => $request->get("type", 1), //1 环比  2 月份同比
                "month" => $request->get("month", ""),  //当前开始时间

                "startDate" => $request->get("startDate", ""),  //当前开始时间
                "endDate" => $request->get("endDate", ""),//当前结束时间
                "startDateBefore" => $request->get("startDateBefore", ""),// 上个阶段的开始时间
                "endDateBefore" => $request->get("endDateBefore", ""),  //上个阶段的结束时间
                //比例筛选
                "trend_rate_min" => $request->get("trend_rate_min", -100000),
                "trend_rate_max" => $request->get("trend_rate_max", -100000),
                "page" => (int)$request->get("page", 1),
                "pagesize" => (int)$request->get("pagesize", 20)
            ];
            $countService = new UserBillCountService();
//            PrintSqlService::listen();
            $data = $countService->pstoreTrend($where);
            return $this->successResponse("ok",$data);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    public function agentTrend(Request $request): string
    {
        try {

            $user = $this->parseToken();
            $agent_id = $user->user_id;  //查找下级所有的数据
            if (!empty($request->get("agent_id"))) {
                $agent_id = $request->get("agent_id");
            }
            $userService = new UserService();
            list($idValues, $column) = $userService->handleUserAccountRelation((int)$agent_id);
            unset($column);
            list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
            $agent_id_column = "agent1_id";
            if (!empty($agent2_id)) {
                $agent_id_column = "agent2_id";
            }
            if (!empty($agent3_id)) {
                $agent_id_column = "agent3_id";
            }
            if (!empty($agent4_id)) {
                $agent_id_column = "agent4_id";
            }
            $where = [
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
                "curHdIndex" => 0,

                "agent_id_column" => $agent_id_column,

                "startDate" => $request->get("startDate", ""),  //当前开始时间
                "endDate" => $request->get("endDate", ""),//当前结束时间

                "startDateBefore" => $request->get("startDateBefore", ""),// 上个阶段的开始时间
                "endDateBefore" => $request->get("endDateBefore", ""),  //上个阶段的结束时间
                //比例筛选
                "trend_rate_min" => $request->get("trend_rate_min", -100000),
                "trend_rate_max" => $request->get("trend_rate_max", -100000),

                "page" => (int)$request->get("page", 1),
                "pagesize" => (int)$request->get("pagesize", 20)
            ];
            $countService = new UserBillCountService();
            $data = $countService->agentTrend($where);

            return $this->successResponse("ok",$data);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }

    }

    public function pstoreTrendExport(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $agent_id = $user->user_id;  //查找下级所有的数据
            if (!empty($request->get("agent_id"))) {
                $agent_id = $request->get("agent_id");
            }
            $userService = new UserService();
            list($idValues, $column) = $userService->handleUserAccountRelation((int)$agent_id);
            unset($column);
            list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
            $where = [
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
                "store_pid" => $request->get("store_pid", ""),
                "curHdIndex" => 0,
                "type" => $request->get("type", 1), //1 环比  2 月份同比
                "month" => $request->get("month", ""),  //当前开始时间
                "startDate" => $request->get("startDate", ""),  //当前开始时间
                "endDate" => $request->get("endDate", ""),//当前结束时间

                "startDateBefore" => $request->get("startDateBefore", ""),// 上个阶段的开始时间
                "endDateBefore" => $request->get("endDateBefore", ""),  //上个阶段的结束时间
                //比例筛选
                "trend_rate_min" => $request->get("trend_rate_min", -100000),
                "trend_rate_max" => $request->get("trend_rate_max", -100000),
//
            ];
            $countService = new UserBillCountService();
            $data = $countService->pstoreTrendExport($where);
            return $this->successResponse("ok", ["data" => $data]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    public function agentTrendExport(Request $request): string
    {
        try {

            $user = $this->parseToken();
            $agent_id = $user->user_id;  //查找下级所有的数据
            if (!empty($request->get("agent_id"))) {
                $agent_id = $request->get("agent_id");
            }
            $userService = new UserService();
            list($idValues, $column) = $userService->handleUserAccountRelation((int)$agent_id);
            unset($column);
            list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
            $agent_id_column = "agent1_id";
            if (!empty($agent2_id)) {
                $agent_id_column = "agent2_id";
            }
            if (!empty($agent3_id)) {
                $agent_id_column = "agent3_id";
            }
            if (!empty($agent4_id)) {
                $agent_id_column = "agent4_id";
            }
            $where = [
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
                "curHdIndex" => 0,

                "agent_id_column" => $agent_id_column,

                "startDate" => $request->get("startDate", ""),  //当前开始时间
                "endDate" => $request->get("endDate", ""),//当前结束时间

                "startDateBefore" => $request->get("startDateBefore", ""),// 上个阶段的开始时间
                "endDateBefore" => $request->get("endDateBefore", ""),  //上个阶段的结束时间
                //比例筛选
                "trend_rate_min" => $request->get("trend_rate_min", -100000),
                "trend_rate_max" => $request->get("trend_rate_max", -100000),

                "page" => (int)$request->get("page", 1),
                "pagesize" => (int)$request->get("pagesize", 20)
            ];
            $countService = new UserBillCountService();
            $data = $countService->agentTrendExport($where);

            return $this->successResponse("ok", [
                "data" => $data ?? []
            ]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }
}